﻿(function () {
    $(function () {
        var _hazardSourceService = abp.services.app.hazardSource;

        var _userService = abp.services.app.user;

        var _$modal = $("#RiskFactorEvaluationModal");
        var _$form = _$modal.find("form");
     
        //保存
        _$form.find('button[type=submit]').click(function (e) {
            e.preventDefault();

            if (!_$form.valid()) {
                return;
            }
         
            var hazardsource = _$form.serializeFormToObject();
            
            abp.ui.setBusy(_$modal);

            _hazardSourceService.createOrUpdateHazardSource({ hazardsource}).done(function () {
                _$modal.modal('hide');
                location.reload(true); //reload page to see edited user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });



        //刷新页面
        $("#RefreshButton").click(function () {
            refreshpost()
        });

        function refreshpost() {
            location.reload();
        }


        //打开编辑窗口，传递参数HazardSourceId
        $('#RiskFactorEvaluationModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget); // Button that triggered the modal
            var hazardsourceId = button.data('hazardsource-id'); // Extract info from data-* attributes
         
            var modal = $(this);
            modal.find('#Id').val(hazardsourceId);

            //查询所有的人员
            _userService.getUsers({ MaxResultCount: 10, SkipCount: 0 })
                .done(function (data) {

               // alert(data.items[0].name + "编号：" + data.items[0].id);

                    var userdropdownlist = _$form.find("#ResponsiblePerson");

                    //清空原来的项目内容
                    $("option", userdropdownlist).remove(); 

                for (var i = 0; i < data.items.length; i++) {
                    var text = data.items[i].name;
                    var value = data.items[i].id;
                    userdropdownlist.append("<option value=" + value + ">" + text + "</option>"); //为Select追加一个Option(下拉项)
                };
                _$form.find("#ResponsiblePerson").selectpicker('refresh');
            });


        })

        //模态框关闭时，清除其中的内容。
        _$modal.on("hide.bs.modal", function () {
            _$form[0].reset();
        });
    });
})();